package com.alstudio.utils.android.net;

import java.util.ArrayList;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.alstudio.utils.log.Mylog;

public class ALConnectionManager {

	private static NetworkInfo mActivieNetworkInfo;
	private static NetworkInfo mLastActiveNetworkInfo;
	private static ALConnectionType mNetworkType = ALConnectionType.TYPE_NONE;
	private static ArrayList<ALNetworkChangedListener> listeners = new ArrayList<ALNetworkChangedListener>();

	public static void updateConnectionInfo(NetworkInfo newNetworkInfo) {
		mLastActiveNetworkInfo = mActivieNetworkInfo;
		mActivieNetworkInfo = newNetworkInfo;
		if (getConnectionType() != ALConnectionType.TYPE_NONE) {
			if (mLastActiveNetworkInfo == null) {
				Mylog.e("data connection established: " + getConnectionType());
				notifyDataConnectionEstablished(getConnectionType());
			} else {
				if (getLastConnectionType() == getConnectionType()) {
					Mylog.d("there is no need to reconnect");
				}
			}

		} else {
			Mylog.e("data connection loss!");
			notifyDataConnectionLost();
			mLastActiveNetworkInfo = null;
		}
	}

	public static boolean isNetWorkConnected(Context ctx) {
		boolean isConnected = false;
		ConnectivityManager connMgr = (ConnectivityManager) ctx
				.getSystemService(Context.CONNECTIVITY_SERVICE);

		NetworkInfo activeInfo = connMgr.getActiveNetworkInfo();
		mLastActiveNetworkInfo = mActivieNetworkInfo;
		mActivieNetworkInfo = activeInfo;
		if (activeInfo != null && activeInfo.isConnected()) {
			isConnected = true;
		} else {
		}
		return isConnected;
	}

	public static ALConnectionType getConnectionType() {
		if (mActivieNetworkInfo != null) {
			if (mActivieNetworkInfo.isConnected()) {
				if (mActivieNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
					mNetworkType = ALConnectionType.TYPE_WIFI;
				} else {
					mNetworkType = ALConnectionType.TYPE_MOBILE;
				}
				return mNetworkType;
			}
		}
		mNetworkType = ALConnectionType.TYPE_NONE;
		return mNetworkType;
	}

	public static ALConnectionType getLastConnectionType() {

		if (mLastActiveNetworkInfo != null) {
			ALConnectionType lastNetworkType = ALConnectionType.TYPE_NONE;
			if (mLastActiveNetworkInfo.isConnected()) {
				if (mLastActiveNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
					lastNetworkType = ALConnectionType.TYPE_WIFI;
				} else {
					lastNetworkType = ALConnectionType.TYPE_MOBILE;
				}
				return lastNetworkType;
			}
		}
		return ALConnectionType.TYPE_NONE;
	}

	public static void AddNetworkChangedListener(
			ALNetworkChangedListener listener) {
		if (!listeners.contains(listener)) {
			listeners.add(listener);
		}
	}

	public static void removeNetworkChangedListener(
			ALNetworkChangedListener listener) {
		if (listeners.contains(listener)) {
			listeners.remove(listener);
		}
	}

	private static void notifyDataConnectionEstablished(ALConnectionType type) {
		for (ALNetworkChangedListener l : listeners) {
			l.onDataConnectionEstablished(type);
		}
	}

	private static void notifyDataConnectionLost() {
		for (ALNetworkChangedListener l : listeners) {
			l.onDataConnectionLost();
		}
	}

	public enum ALConnectionType {
		TYPE_NONE, TYPE_WIFI, TYPE_MOBILE,
	}

}
